
const color = [
    [ '#00d887','rgba(0, 216, 135, 0.4)','rgba(0, 216, 135, 0.1)'],
    [ '#0184d5','rgba(1, 132, 213, 0.4)','rgba(1, 132, 213, 0.1)'],
    [ '#3B2667','rgba(188,120,236,0.4)','rgba(188,120,236, 0.1)'],
    [ '#F8D800','rgba(248,216,0, 0.4)','rgba(248,216,0, 0.1)'],
    [ '#EA5455','rgba(234,84,85, 0.4)','rgba(234,84,85, 0.1)'],
    [ '#32CCBC','rgba(50,204,188, 0.4)','rgba(50,204,188, 0.1)'],
    [ '#DE4313','rgba(222,67,19, 0.4)','rgba(222,67,19, 0.1)'],
    [ '#3CD500','rgba(60,213,0, 0.4)','rgba(60,213,0, 0.1)'],
    [ '#C346C2','rgba(195,70,194, 0.4)','rgba(195,70,194, 0.1)'],
    [ '#360940','rgba(54,9,64, 0.4)','rgba(54,9,64, 0.1)'],
]
templateViewInit = function () {
    onCountdownEnd();
}

// 初始倒计时秒数
const initialSeconds = 300;
var remainingSeconds = initialSeconds;
var timeoutId;

// 显示倒计时的函数
function updateCountdownDisplay() {
    const minutes = Math.floor(remainingSeconds / 60);
    const seconds = remainingSeconds % 60;
    const display = `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
    document.getElementById('countdown').textContent = display;
}

// 开始倒计时的函数
function startCountdown() {
    let that = this;
    timeoutId = setTimeout(function (){
        if (remainingSeconds > 0) {
           remainingSeconds--;
            updateCountdownDisplay();
            startCountdown()
        } else {
            remainingSeconds = initialSeconds;
            clearTimeout(timeoutId);
            // 触发指定的方法
            onCountdownEnd();
        }
    }, 1000);
}

// 倒计时结束时触发的方法
function onCountdownEnd() {
    top.tools.ajax({
        url: '/system/redis/info',
        type: 'POST',
        dataType: 'json',
        loading:false,
        successFun: function (res) {
            if (res.code == 200) {
                osInfo(res.data);
                updateCountdownDisplay();
                startCountdown();
            }
        },
        errorFun:function (){
            updateCountdownDisplay();
            startCountdown();
        }
    });
    top.tools.ajax({
        url: '/system/redis/keys',
        type: 'POST',
        dataType: 'json',
        loading:false,
        successFun: function (res) {
            if (res.code == 200) {
                console.log(res.data)
                var html = '<table class="layui-table"><tbody>';
                layui.each(res.data,function (index,item){
                    html += '<tr><td>'+item+'</td></tr>';
                })
                html += '</tbody></table>';
                $('#keys').html(html);
                $('#keys').removeClass('skeleton');
            }
        }
    });
}




function osInfo(info) {
    $('#redisinfo').html('<table class="layui-table">\n' +
        '                                <colgroup>\n' +
        '                                    <col width="30%">\n' +
        '                                    <col width="70%">\n' +
        '                                </colgroup>\n' +
        '                                <tbody>\n' +
        '                                <tr><td>刷新计时</td><td id="countdown">05:00</td></tr>\n' +
        '                                <tr><td>系统</td><td>'+info.os+'</td></tr>\n' +
        '                                <tr><td>Redis版本</td><td>'+info.redis_version+'</td></tr>\n' +
        '                                <tr><td>进程ID</td><td>'+info.process_id+'</td></tr>\n' +
        '                                <tr><td>监听端口</td><td>'+info.tcp_port+'</td></tr>\n' +
        '                                <tr><td>配置文件</td><td>'+info.config_file+'</td></tr>\n' +
        '                                <tr><td>运行时长</td><td>'+secondsToDHMS(info.uptime_in_seconds)+'</td></tr>\n' +
        '                                </tbody>\n' +
        '                            </table>');
    $('#redisinfo').removeClass('skeleton');
    $('#configinfo').html('<table class="layui-table">\n' +
        '                            <colgroup>\n' +
        '                                <col width="30%">\n' +
        '                                <col width="70%">\n' +
        '                            </colgroup>\n' +
        '                            <tbody>\n' +
        '                            <tr><td>内存使用量</td><td>'+bytesToSize(info.used_memory)+'</td></tr>\n' +
        '                            <tr><td>数据内存占比</td><td>'+info.used_memory_dataset_perc+'</td></tr>\n' +
        '                            <tr><td>最大内存</td><td>'+bytesToSize(info.maxmemory)+'</td></tr>\n' +
        '                            <tr><td>内存碎片率</td><td>'+info.mem_fragmentation_ratio+'</td></tr>\n' +
        '                            <tr><td>内存淘汰策略</td><td>'+info.maxmemory_policy+'</td></tr>\n' +
        '                            <tr><td>客户端数量</td><td>'+info.connected_clients+'</td></tr>\n' +
        '                            <tr><td>阻塞客户端数量</td><td>'+info.blocked_clients+'</td></tr>\n' +
        '                            </tbody>\n' +
        '                        </table>');
    $('#configinfo').removeClass('skeleton');
    $('#persistence').html('<table class="layui-table">\n' +
        '                                <colgroup>\n' +
        '                                    <col width="40%">\n' +
        '                                    <col width="60%">\n' +
        '                                </colgroup>\n' +
        '                                <tbody>\n' +
        '                                <tr><td>数据变更次数</td><td>'+info.rdb_changes_since_last_save+'</td></tr>\n' +
        '                                <tr><td>RDB快照成功时间</td><td>'+unixTimestampToNormalFormat(info.rdb_last_save_time)+'</td></tr>\n' +
        '                                <tr><td>RDB快照状态</td><td>'+info.rdb_last_bgsave_status+'</td></tr>\n' +
        '                                <tr><td>是否开启AOF</td><td>'+(info.aof_enabled==1?'开启':'关闭')+'</td></tr>\n' +
        '                                <tr><td>AOF文件大小</td><td>'+bytesToSize(info.aof_current_size)+'</td></tr>\n' +
        '                                <tr><td>AOF重写后的文件大小</td><td>'+bytesToSize(info.aof_base_size)+'</td></tr>\n' +
        '                                <tr><td>是否正在进行AOF重写</td><td>'+(info.aof_rewrite_in_progress==1?'是':'否')+'</td></tr>\n' +
        '                                </tbody>\n' +
        '                            </table>');
    $('#persistence').removeClass('skeleton');
}
function bytesToSize(bytes) {
    if (bytes == 0 || !bytes) return '0 B';
    const k = 1024;
    const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    // 将结果保留两位小数
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
function secondsToDHMS(seconds) {
    // 计算天数
    const days = Math.floor(seconds / (3600 * 24));
    // 计算剩余秒数用于计算小时
    seconds %= (3600 * 24);
    // 计算小时数
    const hours = Math.floor(seconds / 3600);
    // 计算剩余秒数用于计算分钟
    seconds %= 3600;
    // 计算分钟数
    const minutes = Math.floor(seconds / 60);
    // 计算最终剩余的秒数
    seconds %= 60;

    // 格式化输出，如果数值小于 10，前面补 0
    const formattedDays = String(days).padStart(2, '0');
    const formattedHours = String(hours).padStart(2, '0');
    const formattedMinutes = String(minutes).padStart(2, '0');
    const formattedSeconds = String(seconds).padStart(2, '0');

    // 组合成最终的字符串
    return `${formattedDays} 天 ${formattedHours} 时 ${formattedMinutes} 分 ${formattedSeconds} 秒`;
}

function unixTimestampToNormalFormat(timestamp) {
    timestamp = timestamp*1;
    // 处理秒级时间戳（转换为毫秒）
    if (timestamp > 1e10) timestamp *= 1000;

    const date = new Date(timestamp);
    // 提取各时间分量
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    // 组合成目标格式
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

